<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!--  -->
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
    <!-- 字体图标 -->
    <link rel="stylesheet" href="./fontawesome/css/all.min.css" />
    <script src="./fontawesome/js/all.min.js"></script>
    <!--  -->
    <style>
      .dialog {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        top: 0;
        background-color: rgba(0, 0, 0, 0.5);
        /* display: flex; */
        justify-content: center;
        align-items: center;
        display: none;
      }
      .dialog form {
        background-color: white;
      }
    </style>
  </head>
  <body>
    <div>
      <button data-action="add" class="btn btn-primary">
        <i class="fa-solid fa-plus"></i> 添加
      </button>
    </div>
    <div id="error" style="color: red; padding: 10px"></div>
    <div id="box"></div>

    <div class="dialog" id="dialogAdd">
      <form name="formAdd" class="card">
        <div class="card-header">添加</div>
        <div class="card-body">
          <p>
            <label class="form-label">姓名</label>
            <input class="form-control" value="jack" type="text" name="name" />
          </p>
          <p>
            <label class="form-label">邮箱</label>
            <input class="form-control" type="text" name="email" />
          </p>
          <p>
            <label class="form-label">存款</label>
            <input class="form-control" type="number" name="money" />
          </p>
          <p><input type="checkbox" name="marry" /> 已婚</p>
          <p>
            <button
              class="btn btn-primary btn-sm"
              type="button"
              data-action="add_ok"
            >
              <i class="fa-solid fa-check"></i>
              确定
            </button>
            <button
              class="btn btn-secondary btn-sm"
              type="button"
              data-action="add_cancel"
            >
              <i class="fa-solid fa-xmark"></i>
              取消
            </button>
          </p>
        </div>
      </form>
    </div>

    <div class="dialog" id="dialogEdit">
      <form name="formEdit" class="card">
        <div class="card-header">编辑</div>
        <div class="card-body">
          <input type="text" name="id" hidden />
          <p>
            <label class="form-label">姓名</label>
            <input class="form-control" value="jack" type="text" name="name" />
          </p>
          <p>
            <label class="form-label">
              <i class="fa-regular fa-envelope"></i> 邮箱</label
            >
            <input class="form-control" type="text" name="email" />
          </p>
          <p>
            <label class="form-label">存款</label>
            <input class="form-control" type="number" name="money" />
          </p>
          <p><input type="checkbox" name="marry" /> 已婚</p>
          <p>
            <button
              class="btn btn-primary btn-sm"
              type="button"
              data-action="edit_ok"
            >
              <i class="fa-solid fa-check"></i>
              确定
            </button>
            <button
              class="btn btn-secondary btn-sm"
              type="button"
              data-action="edit_cancel"
            >
              <i class="fa-solid fa-xmark"></i>
              取消
            </button>
          </p>
        </div>
      </form>
    </div>

    <script type="module">
      import PocketBase from "./pocketbase.es.js";
      // 增删改查 curd
      function renderTable(data = []) {
        const html = String.raw;
        return html` <table class="table table-striped">
          <thead>
            <tr>
              <th>姓名</th>
              <th>邮箱</th>
              <th>婚姻</th>
              <th>存款</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            ${data
              .map(
                (el) => html` <tr>
                  <td>${el.name}</td>
                  <td>${el.email}</td>
                  <td>${el.marry ? "已婚" : "未婚"}</td>
                  <td>${el.money}</td>
                  <td>
                    <button
                      class="btn btn-info btn-sm"
                      data-action="edit"
                      data-edit-id="${el.id}"
                    >
                      <i class="fa-regular fa-pen-to-square"></i>
                      编辑
                    </button>
                    <button
                      class="btn btn-danger btn-sm"
                      data-action="delete"
                      data-delete-id="${el.id}"
                    >
                      <i class="fa-regular fa-trash-can"></i>
                      删除
                    </button>
                  </td>
                </tr>`
              )
              .join("")}
          </tbody>
        </table>`;
      }

      // 链接数据库
      const pb = new PocketBase("http://127.0.0.1:8090");
      async function getLists() {
        try {
          const data = await pb.collection("employ").getFullList();
          document.getElementById("box").innerHTML = renderTable(data);
        } catch (error) {}
      }
      getLists();

      // 添加
      const dialogAdd = document.getElementById("dialogAdd");
      // 编辑
      const dialogEdit = document.getElementById("dialogEdit");

      const methods = {
        add() {
          console.log("add", this);
        },
        add_cancel() {
          console.log("add_cancel", this);
        },
      };
      document.addEventListener("click", async (e) => {
        const action = e.target.dataset.action;
        if (action === undefined) return;
        if (methods[action] === undefined) return;
        methods[action].call(e.target);
      });
    </script>
  </body>
</html>
